package org.mx.mycrm.web.action;

import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.mx.mycrm.beans.User;
import org.mx.mycrm.beans.page.PageView;
import org.mx.mycrm.beans.page.QueryResult;
import org.mx.mycrm.service.UserService;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class UserListAcion extends ActionSupport {
	@Resource UserService userService;
	private static final long serialVersionUID = -4450952276207046066L;
	private int page = 1;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	@Override
	public String execute() throws Exception {
		PageView<User> pageView = new PageView<User>(5, page);
		int firstindex = (pageView.getCurrentpage() - 1)
				* pageView.getMaxresult();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("role", "desc");
		QueryResult<User> qr = userService.getScrollData(User.class,
				firstindex, pageView.getMaxresult(),"o.visible=?",new Object[]{true}, orderby);
		pageView.setQueryResult(qr);

		ActionContext.getContext().put("pageView", pageView);
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
